﻿using UML_Creator.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UML_Creator.Command
{
    // Bruges til at fjerne punkt fra punkt samlingen, fjerne også tilhørende kanter.
    [CLSCompliant(false)]
    public class RemoveNodesCommand : IUndoRedoCommand
    {
        private ObservableCollection<Diagram> nodes;
        private ObservableCollection<Edge> edges;
        private List<Diagram> removeNodes = new List<Diagram>();
        private List<Edge> removeEdges = new List<Edge>();
        private List<Edge> selectedEdge;

        public RemoveNodesCommand(ObservableCollection<Diagram> _nodes, ObservableCollection<Edge> _edges, List<Diagram> _removeNodes, List<Edge> _selectedEdge) 
        { 
            nodes = _nodes;
            edges = _edges; 
            removeNodes = new List<Diagram>(_removeNodes);
            selectedEdge = _selectedEdge;
            foreach (Diagram d in removeNodes)
            {
                foreach (Edge e in edges)
                {
                    if (e.EndA == d || e.EndB == d)
                    {
                        removeEdges.Add(e);
                    }
                }
            }
        }

        public void Execute()
        {
            foreach (Edge e in removeEdges)
            {
                edges.Remove(e);
                e.Opacity = 1.0;
                e.Colour = "Black";
                selectedEdge.Remove(e);
            }
            foreach (Diagram d in removeNodes) nodes.Remove(d);
        }

        public void UnExecute()
        {
            foreach (Diagram d in removeNodes) nodes.Add(d);
            foreach (Edge e in removeEdges) {

                edges.Add(e);
            }
        }

        public void ReExecute()
        {
            Execute();
        }
    }
}
